// 输入：n = 3
// 输出：[[1, 2, 3], [8, 9, 4], [7, 6, 5]]

let n = 3

var generateMatrix = function (n) {
    let arr = Array.from({ length: n }, () => Array(n).fill(0));
    let x = 0, y = 0;
    let i = 0, j = 0
    let cishu = Math.floor(n / 2)
    let count = 1, offset = 1
    while (cishu) {
        for (j = y; j < n - offset; j++) {
            arr[x][j] = count++
        }
        for (i = x; i < n - offset; i++) {
            arr[i][j] = count++
        }
        for (; j > y; j--) {
            arr[i][j] = count++
        }
        for (; i > x; i--) {
            arr[i][j] = count++
        }
        x++, y++, offset++
        cishu--
    }

    if (n % 2 == 1) {
        arr[x][y] = count
    }

    return arr
}

console.log(generateMatrix(n));
